﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MasterPage master = this.Master as MasterPage;
        master.BodyId = "index";
        //get upcoming competitions

        Load_Upcoming();

        //get ongoing competitions

        Load_Ongoing();

        //get previous competitions

        

    }


    void Load_Upcoming()
    {
        CompetitionDAO comDAO = new CompetitionDAO();
        AwardDAO awardDAO = new AwardDAO();
        dtl_Upcoming.DataSource = comDAO.SelectUpcoming();
        dtl_Upcoming.DataBind();
        if (dtl_Upcoming.Items.Count > 0)
        {
            foreach (DataListItem item in dtl_Upcoming.Items)
            {

                Label competitionID = item.FindControl("lblCompetitionID") as Label;
                int competID = Convert.ToInt32(competitionID.Text);


                DataList datalist = (DataList)item.FindControl("dtl_Awards_Upcoming");
                datalist.DataSource = awardDAO.AwardOfCompetition(competID);
                datalist.DataBind();
            }
        }

    }

    void Load_Ongoing()
    {
        CompetitionDAO comDAO = new CompetitionDAO();
        PostingDAO postDAO = new PostingDAO();
        AwardDAO awardDAO = new AwardDAO();
        dtl_Ongoing.DataSource = comDAO.SelectOngoing();
        dtl_Ongoing.DataBind();
        if (dtl_Ongoing.Items.Count > 0)
        {
            foreach (DataListItem item in dtl_Ongoing.Items)
            {
                Label competitionID = item.FindControl("lblCompetitionID") as Label;
                int competID = Convert.ToInt32(competitionID.Text);                
                DataList datalist = (DataList)item.FindControl("dtl_Awards_Ongoing");
                datalist.DataSource = awardDAO.AwardOfCompetition(competID);
                datalist.DataBind();
                ((Label)item.FindControl("lb_Total")).Text = postDAO.GetTotalSubmissions(competID).ToString() + " Postings";
            }
        }
    }


    protected void btnJoin_Click(object sender, EventArgs e)
    {
        if (Session["roleID"] != null)
        {
            if (Session["roleID"].ToString().Equals("4"))
            {
                Response.Redirect("~/Admin/UploadPosting.aspx");
            }
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Your must Login with Student Account!');", true);
            
    }
}